﻿using System.Windows.Input;
using Ninject.Core.Parameters;
using TestBat.IModels.Domain;
using TestBat.IModels.Models;
using TestBat.IViewModels.Workspaces.Trainees;
using TestBat.ServiceLocator;
using TestBat.ViewModels.Attributes;
using TestBat.ViewModels.Base;
using TestBat.ViewModels.Base.Commands;

namespace TestBat.ViewModels.Workspaces.Trainees
{
    public class TraineeViewModel : ListItemViewModel, ITraineeViewModel
    {
        public TraineeViewModel(ITrainee trainee, ITraineeModel traineeModel)
        {
            this.trainee = trainee;
            this.traineeModel = traineeModel;
            this.availableTraineeGroupList = DefaultServiceLocator.Get<IAvailableTraineeGroupListViewModel>(With.Parameters.ConstructorArgument("trainee", trainee));
            this.associatedTraineeGroupList = DefaultServiceLocator.Get<IAssociatedTraineeGroupListViewModel>(With.Parameters.ConstructorArgument("trainee", trainee));
        }

        public ICommand SaveCommand
        {
            get
            {
                return new RelayCommand(e => Save());
            }
        }

        public IAvailableTraineeGroupListViewModel AvailableTraineeGroupList
        {
            get
            {
                return availableTraineeGroupList;
            }
        }

        public IAssociatedTraineeGroupListViewModel AssociatedTraineeGroupList
        {
            get
            {
                return associatedTraineeGroupList;
            }
        }

        public ICommand AddTraineeGroupCommand
        {
            get
            {
                return new RelayCommand(
                    ctx => AddTraineeGroup(availableTraineeGroupList.Selected.TraineeGroup),
                    ctx => availableTraineeGroupList.Selected != null);
            }
        }

        public ICommand RemoveTraineeGroupCommand
        {
            get
            {
                return new RelayCommand(
                    ctx => RemoveTraineeGroup(associatedTraineeGroupList.Selected.TraineeGroup),
                    ctx => associatedTraineeGroupList.Selected != null);
            }
        }

        [BusinessErrorDialog]
        private void AddTraineeGroup(ITraineeGroup traineeGroup)
        {
            traineeModel.AddTraineeGroup(trainee, traineeGroup);

            associatedTraineeGroupList.RecreateItems();
            availableTraineeGroupList.RecreateItems();
        }

        [BusinessErrorDialog]
        private void RemoveTraineeGroup(ITraineeGroup traineeGroup)
        {
            traineeModel.RemoveTraineeGroup(trainee, traineeGroup);

            associatedTraineeGroupList.RecreateItems();
            availableTraineeGroupList.RecreateItems();
        }

        [BusinessErrorDialog]
        [ValidationErrorDialog]
        private void Save()
        {
            traineeModel.Save(trainee);
        }

        public ITrainee Trainee
        {
            get { return trainee; }
        }

        public override void OnLostFocus()
        {
            // revert changes
            traineeModel.Revert(trainee);

            availableTraineeGroupList.RecreateItems();
            associatedTraineeGroupList.RecreateItems();
        }

        private readonly ITrainee trainee;

        private ITraineeModel traineeModel;

        private readonly IAvailableTraineeGroupListViewModel availableTraineeGroupList;
        private readonly IAssociatedTraineeGroupListViewModel associatedTraineeGroupList;
    }
}
